package com.example.order.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.order.R;
import com.example.order.pojo.Customer;
import com.example.order.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
    private EditText edit_phone;
    private EditText edit_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_login=findViewById(R.id.btn_login);
        Button btn_quit=findViewById(R.id.btn_quit);
        Button btn_submit=findViewById(R.id.btn_submit);
        //Button btn_set=findViewById(R.id.btn_setHost);
        edit_phone=findViewById(R.id.edit_phone);
        edit_pass=findViewById(R.id.edit_pass);
        //CheckBox checkBox=findViewById(R.id.checkbox);
        btn_login.setOnClickListener(this);
        btn_quit.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        //默认ip地址为192.168.0.132
       /* SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        String host = sharedPreferences.getString("host", null);
        if(host==null)
        {
            Utils.showToast(this,"data为空，说明该程序还未初始化服务器IP地址",true);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("data","http://192.168.0.132:8080");
            edit.apply();
        }*/
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
           /* case R.id.btn_setHost:
                startActivity(new Intent(this,Main9Activity.class));
                break;*/
            case R.id.btn_login:
                if(Utils.isNotNullString(edit_phone.getText().toString(),edit_phone.getText().toString()))
                {
                    //不为空
                   // Utils.sendHttpRequest("");
                    if(!Utils.isMobile(edit_phone.getText().toString()))
                    {
                        Utils.showToast(this,"手机号码格式错误,手机号码必须为中华人民共和国大陆地区合法手机号码",true);
                    }
                    else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phone", edit_phone.getText().toString());
                        map.put("password", edit_pass.getText().toString());
                        //final StringBuilder builder=new StringBuilder();
                        //final String str= "";
                        //Utils.showToast(this,map.toString(),true);
                        Utils.sendPostHttpRequest("customer/login", map, new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                //出现异常
                                //Toast.makeText(MainActivity.this,"出现异常",Toast.LENGTH_SHORT).show();
                                //Utils.showToast(MainActivity.this,"出现异常",false);
                                Log.e("MainActivity", Arrays.toString(e.getStackTrace()));
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Utils.showToast(MainActivity.this, "出现异常", false);
                                    }
                                });
                            }

                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                assert response.body() != null;
                                String string = response.body().string();
                                Log.i("MainActivity", string);
                                if (Utils.isNotNullString(string)) {
                                    //不为空
                                    Gson gson = new Gson();
                                    final Customer customer = gson.fromJson(string, new TypeToken<Customer>() {
                                    }.getType());
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Utils.showToast(MainActivity.this, "登录成功" + customer.toString(), true);
                                            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                                            //Customer类可序列化
                                            intent.putExtra("customer",customer);
                                            startActivity(intent);
                                        }
                                    });


                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Utils.showToast(MainActivity.this, "手机号或密码输入错误", false);
                                        }
                                    });

                                }
                            }
                        });
                    }

                }
                else
                {
                    Toast.makeText(this,"手机号或密码均不能为空",Toast.LENGTH_SHORT).show();
                    //Utils.showToast(this,"手机号或密码均不能为空",true);
                }
                break;
            case R.id.btn_quit:
                //取消
                edit_pass.setText("");
                edit_phone.setText("");
                break;
            case R.id.btn_submit:
                //注册
                startActivity(new Intent(this,Main8Activity.class));
                break;
            default:
                break;
        }

    }
}
